package com.wxdc.nmgyt.nmgytwydc.common;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.location.Location;
import android.util.Log;
import android.view.View;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.wxdc.nmgyt.nmgytwydc.IMapFunInterface.IMapFunInterface;
import com.wxdc.nmgyt.nmgytwydc.app.MyApp;

/**
 * Created by Administrator on 2018/5/7.
 */

public class BaiDuMapBasicSetup {
    private final String TAG = this.getClass().getSimpleName();
    private Context mContext;
    private MyApp mApp;
    private BaiduMap mBaiduMap;
    public BaiDuMapBasicSetup(Context context,BaiduMap baiduMap) {
        mContext = context;
        mApp = MyApp.getInstance();
        this.mBaiduMap = baiduMap;
    }

    public void initMap(Location location,RxPermissions rxPermissions) {
        setMapUi();
        rxPermissions.request(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
                .subscribe(granted -> {
                    Log.i(TAG, "initMap:------------------------> "+granted);
                    if (granted) {
                        initLocPosition(location);
                    } else {
                        Log.i(TAG, "initMap: --------------");
                    }
                });
    }

    /*定位初始化*/
    private Double lastX = 0.0;
    private int mCurrentDirection = 0;
    private float mCurrentAccracy;
    private MyLocationData locData;
    private double mCurrentLat = 0.0;
    private double mCurrentLon = 0.0;

    private void initLocPosition(Location location) {
        if (location == null) {
            return;
        }
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        // 构造定位数据  accuracy:定位精度;direction:GPS定位时方向角度
        LatLng mLatLng = mApp.latLngConVert(new LatLng(location.getLatitude(), location.getLongitude()));
        if (mLatLng == null) {
            return;
        }
        mCurrentAccracy = 100;//当前定位精度
        mCurrentLat = mLatLng.latitude;
        mCurrentLon = mLatLng.longitude;
        locData = new MyLocationData.Builder()
                .accuracy(mCurrentAccracy)
                // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(mCurrentDirection).latitude(mCurrentLat)
                .longitude(mCurrentLon).build();
        // 设置定位数据
        mBaiduMap.setMyLocationData(locData);
        // 设置定位图层的配置（定位模式，是否允许方向信息，用户自定义定位图标）
        MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, null);
        mBaiduMap.setMyLocationConfiguration(config);
        mapStatus(location,16.0f);
    }
    /**/
    public void setSensor(SensorEvent event){
        double x = event.values[SensorManager.DATA_X];
        if (Math.abs(x - lastX) > 1.0) {
            mCurrentDirection = (int) x;
            locData = new MyLocationData.Builder()
                    .accuracy(mCurrentAccracy)
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(mCurrentDirection).latitude(mCurrentLat)
                    .longitude(mCurrentLon).build();
            mBaiduMap.setMyLocationData(locData);
        }
        lastX = x;
    }

    /*设置地图UI控制器*/
    private void setMapUi() {
        if (mBaiduMap != null) {
            UiSettings uiSettings = mBaiduMap.getUiSettings();
            uiSettings.setAllGesturesEnabled(true);//设置是否允许所有手势操作
            uiSettings.setCompassEnabled(true);//设置是否允许指南针(默认允许true，当地图发生旋转和视角变化时，默认出现在左上角的指南针)
        }
    }

    /*以动画方式更新地图状态*/
    private void mapStatus(Location location, float level) {
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.target(mApp.latLngConVert(new LatLng(location.getLatitude(), location.getLongitude()))).zoom(level);
        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
    }
    /*定位 Btn click*/
    public  class MyLocListener implements View.OnClickListener {
        private Location location;
        public MyLocListener(Location location) {
            this.location = location;
        }
        @Override
        public void onClick(View view) {
            if (location == null) {
                location = mApp.getMyBinder().getLocation();
                mApp.showToastShortMessage(mContext, "无法获取定位信息，请检查GPS是否打开");
                return;
            }
            if (!mBaiduMap.isMyLocationEnabled()) {
                initLocPosition(location);
            } else {
                mapStatus(location,18.0f);
            }

        }
    }

}
